home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PACKET / CBBS60SO.ZIP / MBGATE.C < prev    next >
Text File  |  1989-01-26  |  9KB  |  425 lines

  1.  
  2. /*
  3.  *  MBGATE.C - 1/26/88 - Gateway routines.
  4.  *                      - by David B. Toth, VE3GYQ
  5.  *                      - if it doesn't work, you can blame it on
  6.  *                         somebody else
  7.  *
  8.  */
  9.  
  10. #include "mb.h"
  11.  
  12. char *gm[num_gm];
  13.  
  14. static char *lm = "*** LINKED to $S\n";
  15.  
  16. PORTS *okport(id)
  17. char id;
  18. {
  19.   register PORTS *p;
  20.  
  21.   if ((p = findport(id)) is NULL) { port->msg = mnport; return NULL; }
  22.   if (p is port)                  { port->msg = mcant;  return NULL; }
  23.   if (p->mode isnt idle)          { port->msg = minuse; return NULL; }
  24.   return p;
  25. }
  26.  
  27. /*
  28.  *  C Command: connect to someone in the user file.
  29.  *  tcall is passed as the eventual target station, with its
  30.  *  user record in tuser.
  31.  */
  32.  
  33. connuser()
  34. {
  35.   register PORTS *s;
  36.   register short link;
  37.   char gatecall[ln_call];
  38.  
  39.   pcall(tcall, port->fld[1]);
  40.   if (!rduser(tcall, tuser)) { port->msg = mfind; return; }
  41.  
  42. /*
  43.  *  If user linked thru a GateWay, connect to the gateway.
  44.  */
  45.  
  46.   link = false;
  47.   if (tuser->port is 'L')
  48.   {
  49.     if (!*tuser->path) { port->msg = mcant; return; }
  50.     pcall(gatecall, tuser->path);
  51. /*
  52.  *  Read GateWay's call into a user record.
  53.  */
  54.     if (!rduser(gatecall, tuser)) { port->msg = mfind; return; }
  55.     link = true;
  56.   }
  57.  
  58.   if ((s = okport(tuser->port)) is NULL) return;
  59.   if (port->mode & remote) if (!(s->priv & p_gate))
  60.   { port->msg = gm[0]; return; }
  61.  
  62.   if (link) rduser(gatecall, s->user); else rduser(tcall, s->user);
  63.  
  64. /*
  65.  *  Build the connect line to send to the tnc.
  66.  */
  67.  
  68.   unbl(port->line, tuser->call, ln_call);
  69.  
  70.   if (*tuser->path)
  71.     sprintf(tmp->scr, "C %s-%d V %s\n", port->line, tuser->ssid, tuser->path);
  72.   else
  73.     sprintf(tmp->scr, "C %s-%d\n", port->line, tuser->ssid);
  74.  
  75.   s->ec = s->ecuser;
  76.   ctnc(s, link);
  77.   s->ec = s->ecmon;
  78.   log ('G', 'E', ' ', nullstr);
  79. }
  80.  
  81. /*
  82.  *  C Command: make the connection between the master and slave ports
  83.  *  when the user specifies the port and connect path.
  84.  */
  85.  
  86. ctncv()
  87. {
  88.   register PORTS *s;
  89.  
  90.   if ((s = okport(port->opt2)) is NULL) return;
  91.   if (!(s->priv & p_gate)) { port->msg = gm[0]; return; }
  92.  
  93.   pcall(tcall, port->fld[1]);
  94.   rduser(tcall, s->user);
  95.  
  96.   if (port->flds > 2)
  97.     sprintf(tmp->scr,"C %s V %s\n", port->fld[1], port->fld[3]);
  98.   else sprintf(tmp->scr, "C %s\n", port->fld[1]);
  99.  
  100.   s->ec = s->ecuser;
  101.   ctnc(s, false);
  102.   s->ec = s->ecmon;
  103.   log ('G', 'E', ' ', nullstr);
  104. }
  105.  
  106. ctnc(s, link)
  107. PORTS *s;
  108. short link;
  109. {
  110.   register PORTS *m;
  111.   int open_gate;
  112.   m = port;
  113.   m->lport = s;
  114.   s->lport = m;
  115.  
  116. /*
  117.  *  Send the connect string to the tnc.
  118.  */
  119.  
  120.   s->mode = remote;
  121.   ioport(s);
  122.   contnc(tmp->scr);
  123.  
  124. /*
  125.  *  Tell the user "attempting connect".
  126.  */
  127.  
  128.   ioport(m);
  129.   prtx(gm[2]);
  130.  
  131. /*
  132.  *  Special case for in-memory link, in case contnc failed.
  133.  */
  134.  
  135.   if (s->mode is idle)
  136.   {
  137.     m->msg = gm[3];
  138.     return;
  139.   }
  140.  
  141. /*
  142.  *  Wait for the connection.
  143.  */
  144.  
  145.   settmr(&m->expire, 60);
  146.   while (chktmr(m->expire))
  147.   {
  148. /*
  149.  *  User wants to give up?
  150.  */
  151.     ioport(m);
  152.     open_gate = false;
  153.     if (instat()) if(getdat()) if (*m->line is ctl_w) open_gate = true;
  154.     if (!isdcd()) open_gate = true;
  155.     if (open_gate)
  156.     {
  157.       log ('G', 'A', m->opt2, m->fld[1]);
  158.       ioport(s); distnc();
  159.       s->mode = idle;
  160.       ioport(m);
  161.       m->msg = gm[5];
  162.       return;
  163.     }
  164. /*
  165.  *  Something from the slave tnc.
  166.  */
  167.     ioport(s);
  168.     if (instat()) if (getdat())
  169.     {
  170.       if (isdis(s->line))
  171.       {
  172.         waitcmd(0);
  173.         s->mode = idle;
  174.         ioport(m);
  175.         log ('G', 'A', m->opt2, m->fld[1]);
  176.         m->msg = gm[3];
  177.         return;
  178.       }
  179.  
  180.       else if (iscon(s->line) or (s->dev is p_serial))
  181.       {
  182.         log ('G', 'C', m->opt2, m->fld[1]);
  183.  
  184.         if (m isnt cport)
  185.         {
  186.           ioport(m); prtx(gm[4]); prtx(lm);
  187.           ioport(s); wait(10); prtx(lm);  /* wait 10 sec. to send msg */
  188.         }
  189.  
  190.         if (link)
  191.         {
  192.           outstr("G\nC "); outnb(tcall, ln_call); outchar('\n');
  193.         }
  194.  
  195. /*
  196.  *  Special case of connecting
  197.  *  from the console.
  198.  */
  199.         if (m is cport)
  200.         {
  201.           m->fl = NULL;
  202.           term(s);
  203.           ioport(s);
  204.           tncstate();
  205.           if (s->mode is discon)
  206.           {
  207.             s->mode = idle;
  208.             m->lport = cport; s->lport = cport;
  209.             ioport(m);
  210.             return;
  211.           }
  212.           else    /* just go back to cmd line and leave slave connected */
  213.           {
  214.             convtnc();
  215.             s->mode = idle;
  216.             ioport(m);
  217.             return;
  218.           }
  219.         }
  220.  
  221.         else tncslinked(m, s);
  222.  
  223.         ioport(s);
  224.         distnc();
  225.         s->mode = idle;
  226.         m->lport = cport; s->lport = cport;
  227.         ioport(m);
  228.         return;
  229.       }
  230.     }
  231.   }
  232. /*
  233.  *  Fall through if timer times out.
  234.  */
  235.   ioport(s);
  236.   distnc();
  237.   s->mode = idle;
  238.   ioport(m);
  239.   log ('G', 'A', port->opt2, port->fld[1]);
  240.   port->msg = gm[3];
  241. }
  242.  
  243. /*
  244.  *  M command: Monitor the slave port from the master port.
  245.  *  return if anything from the master port.
  246.  */
  247.  
  248. mtncv()
  249. {
  250.   register PORTS *m, *s;
  251.   register int linecount;
  252.  
  253.   if (port->opt2 is ' ') { shports(); return; }
  254.   if ((s = okport(port->opt2)) is NULL) return;
  255.   if (!(s->priv & p_mon)) { port->msg = gm[0]; return; }
  256.  
  257.   m = port;
  258.   m->lport = s;
  259.   s->lport = m;
  260.   log ('G', 'M', port->opt2, nullstr);
  261.   prtx(gm[6]);
  262.   ioport(s);
  263.   onecmd(t_mon);
  264.   settmr(&s->expire, s->mtime);
  265.   linecount = s->mcount;
  266.   s->ec = s->ecuser;
  267.   while (linecount and chktmr(s->expire))
  268.   {
  269.     ioport(s);
  270.     if (instat()) if (getdat())
  271.     {
  272.       ioport(m);
  273.       outstr(s->line);
  274.       linecount--;
  275.     }
  276.  
  277.     ioport(m);
  278.     if (m->flags & p_trans) if(!isdcd()) linecount = 0;
  279.     if (instat()) if (getdat()) linecount = 0;
  280.   }
  281.  
  282.   s->ec = s->ecmon;
  283.   ioport(s);
  284.   onecmd(t_moff);
  285.   ioport(m);
  286.   m->lport = cport;
  287.   s->lport = cport;
  288.   log ('G', 'E', ' ', nullstr);
  289. }
  290.  
  291. /*
  292.  *  U Command: master port sends unproto frames through the
  293.  *  selected slave port. Sets slave port CONOK ON to allow a
  294.  *  connect to occur from the slave port.
  295.  */
  296.  
  297. utncv()
  298. {
  299.   register PORTS *m, *s;
  300.  
  301.   if (port->opt2 is ' ') { shports(); return; }
  302.   if ((s = okport (port->opt2)) is NULL) return;
  303.   if (!(s->priv & p_mon)) { port->msg = gm[0]; return; }
  304.  
  305.   m = port;
  306.   log ('G', 'U', m->opt2, nullstr);
  307.   m->lport = s;
  308.   s->lport = m;
  309.   s->ec = s->ecuser;
  310.   utnc (m, s);
  311.   s->ec = s->ecmon;
  312.   s->mode = idle;
  313.   m->lport = cport;
  314.   s->lport = cport;
  315.   ioport(m);
  316.   log ('G', 'E', ' ', nullstr);
  317. }
  318.  
  319. utnc(m, s)
  320. PORTS *m, *s;
  321. {
  322.   prtx(gm[1]);
  323.   ioport(s);
  324.   onecmd(t_con);
  325.   convtnc();
  326.   settmr(&m->expire, m->ctime);
  327.   while(chktmr(m->expire))
  328.   {
  329.     if (xtalk(m, s))
  330.     {
  331.       if ((*m->line is ctl_w) or (m->mode & discon))
  332.       {
  333.         ioport(s); cmdtnc(); onecmd(t_coff);
  334.         return;
  335.       }
  336.     }
  337.  
  338.     ioport(s);
  339.     if (instat())
  340.     {
  341.       getcmd();
  342.       if (iscon(s->line))
  343.       {
  344.         s->mode = remote;
  345.         pcall(tcall, port->fld[3]);
  346.         rduser(tcall, s->user);
  347.         ioport(m); prtx(lm); ioport(s); prtx(lm);
  348.         tncslinked(m, s);
  349.         ioport(s); cmdtnc(); onecmd(t_coff);
  350.         return;
  351.       }
  352.     }
  353.   }
  354.   m->mode = timeout;
  355. }
  356.  
  357. /*
  358.  * xtalk: handles the cross-connection of data between tncs.
  359.  * fails with loss of tnc on either side ... lets calling fn
  360.  * clean up the tncs.
  361.  */
  362.  
  363. xtalk(pf, pt)
  364. PORTS *pf, *pt;
  365. {
  366.   ioport(pf);
  367.   if (instat()) if (getdat())
  368.   {
  369.     if (!(pf->mode & discon)) if (!isdis(pf->line) and (*pf->line isnt ctl_w))
  370.     { ioport(pt); outstr(pf->line); }
  371.     return true;
  372.   }
  373.   if (pf->flags & p_trans)
  374.     if (!isdcd())
  375.       {
  376.       pf->mode = discon;
  377.       return true;
  378.       }
  379.   return false;
  380. }
  381.  
  382. /*
  383.  * tncslinked: always disconnects slave and sets it to idle before
  384.  * returning ... lets calling routine take care of master port
  385.  */
  386.  
  387. tncslinked(m, s)
  388. PORTS *m, *s;
  389. {
  390.   settmr(&m->expire, m->ctime);
  391.   while(chktmr(m->expire))
  392.   {
  393.     if (xtalk(s, m))
  394.     {
  395.       if (s->mode & discon)
  396.       {
  397.         ioport(s); cmdtnc(); s->mode = idle; return;
  398.       }
  399.       else
  400.       {
  401.         settmr(&m->expire, m->ctime);
  402.       }
  403.     }
  404.  
  405.     if (xtalk(m, s))
  406.     {
  407.       if ((m->mode & discon) or (*m->line is ctl_w))
  408.       {
  409.         ioport(s); distnc(); s->mode=idle; return;
  410.       }
  411.       else
  412.       {
  413.         settmr(&m->expire, m->ctime);
  414.       }
  415.     }
  416.   }
  417.  
  418. /*
  419.  *  On timeout, fall thru, let caller say timeout and dump user.
  420.  */
  421.  
  422.   ioport(s); distnc(); s->mode = idle;
  423.   ioport(m); m->mode = timeout;
  424. }
  425.